﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_profitBusiness : BaseListPage
{
 
    protected void Page_Load(object sender, EventArgs e)
    {

        if (user == null ||role.RoleId >2||role.RoleId<1)
        {
            UtilityMessageBox.Show(this, "您没有权限操作");
            return;
        }
        PageSize = 10;
        TableName = "Profit"; Pk = "ProfitId";
        Condition = " ";
        if (!this.IsPostBack)
        {
            try
            {
               int userId = user.UserId;
               if (role.RoleId == 2)
                {
                    btnSearch.Visible = false;
                    searchName.Visible = false;
                    textName.Visible = false;
                    Condition = Condition + "and ListId in (SELECT ListId from Activity where SellerID = "+user.UserId+")";
                }
            }
            catch {  }
            PageIndex = 0;
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
        }
    }

    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        PageIndex = Pager.CurrentPageIndex - 1;
        BindData(ref rptList);
    }
  

  
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string name = textName.Text.Trim();
        try
        {
            if (name != "")
            {
                Condition = Condition + "and ListId in (";
                int sellerId = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == name).UserId;
                List<Activity> list = ActivityBLL.GetInstance().GetList(r => r.SellerId == sellerId);
                foreach (var l in list)
                {
                    Condition = Condition + l.ListId + " ,";
                }
                Condition = Condition.Substring(0, Condition.Length - 1);
                Condition = Condition + ")";
            }
        }
        catch
        {
            UtilityMessageBox.Show(this, "输入商家登入名不存在");
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);

    }

    public string getPlayStatus(object o, object oo)
    {
        string result = "异常";
        int id = Convert.ToInt32(o);
        switch (id)
        {
            case 0: result = "待支付"; break;
            case 1: result = "支付成功"; break;
        }
        return result;
    }

    public String getSellerName(object o)
    {
        string result = "异常";
        try
        {
            int listId = Convert.ToInt32(o);
            int sellerId = ActivityBLL.GetInstance().GetEntity(r => r.ListId == listId).SellerId;
            result = SystemUserBLL.GetInstance().GetEntity(r => r.UserId == sellerId).LoginName;
        }
        catch { }
        return result;
    }

    public string getOperation(object o, object oo)
    {
        string result = "异常";
        try
        {
            int id = Convert.ToInt32(o);
            if (id == 0)
            {
                int listId = Convert.ToInt32(oo);
                if (Convert.ToInt32(Session["RoleId"]) == 1)
                {
                    result = "<a href='profitPay.aspx?listId=" + listId + "'>去支付</a>";
                }
                else
                {
                    Activity a = ActivityBLL.GetInstance().GetEntity(r => r.ListId == listId);
                    SystemUser user = SystemUserBLL.GetInstance().GetEntity(r => r.UserId == a.ManagerId);
                    result = "<a href='#'>联系负责人电话：" + user.UserPhone + "支付</a>";
                }
            }
            else if (id == 1)
            {
                result = "活动结束";
            }
        }
        catch { }
        return result;
    }

    public double getMoney()
    {
        double result = 0;

        try
        {
            
            if (role.RoleId == 1)
            {
                List<Profit> list;
                list = ProfitBLL.GetInstance().GetList();
                foreach(var l in list)
                {
                    result += l.PlatformProfit;
                }
            }
            else if (role.RoleId == 2)
            {
                var temp=ActivityBLL.GetInstance().GetList(r=>r.Status==6&&r.SellerId==user.UserId);
               
                foreach(var t in temp)
                {
                    result+=ProfitBLL.GetInstance().GetEntity(r=>r.ListId==t.ListId).SellerProfit;
                }
            }
        }
        catch(Exception e)
        {
            result = 0;
        }

        return result;
    }
}